<template>
	<view class="notice-detail-container">

		<!-- 通知详情内容 -->
		<view class="notice-content" v-if="noticeDetail && !loading">
			<view class="notice-header">
				<text class="notice-title">{{ noticeDetail.title }}</text>
				<text class="notice-time">{{ formatTime(noticeDetail.send_time) }}</text>
				<text class="notice-type">{{ getTypeText(noticeDetail.type) }}</text>
			</view>

			<view class="notice-body">
				<rich-text :nodes="noticeDetail.content"></rich-text>
			</view>
		</view>

		<!-- 加载中状态 -->
		<view class="loading-state" v-if="loading">
			<text>加载中...</text>
		</view>

		<!-- 错误状态 -->
		<view class="error-state" v-if="error && !loading">
			<text>{{ error }}</text>
			<button class="retry-btn" @click="loadNoticeDetail">重试</button>
		</view>
	</view>
</template>

<script>
	import {
		getNoticeDetail
	} from '../../api/request.js';

	export default {
		data() {
			return {
				noticeDetail: null,
				loading: false,
				error: ''
			};
		},

		onLoad(options) {
			// 获取从上一个页面传来的id参数
			this.noticeId = options.id;
			if (this.noticeId) {
				this.loadNoticeDetail();
			} else {
				this.error = '通知ID不存在';
			}
		},

		methods: {
			// 加载通知详情
			loadNoticeDetail() {
				if (!this.noticeId) return;

				this.loading = true;
				this.error = '';

				getNoticeDetail({
					id: this.noticeId
				}).then(res => {
					if (res.code === 1 && res.data) {
						this.noticeDetail = res.data;
					} else {
						this.error = res.msg || '获取通知详情失败';
						uni.showToast({
							title: this.error,
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取通知详情失败:', err);
					this.error = '网络错误';
					uni.showToast({
						title: err.msg || '网络错误',
						icon: 'none'
					});
				}).finally(() => {
					this.loading = false;
				});
			},

			// 返回上一页
			navigateBack() {
				uni.navigateBack();
			},

			// 格式化时间
			formatTime(timeStr) {
				if (!timeStr) return '';
				// 这里可以根据需要格式化时间
				return timeStr;
			},

			// 获取通知类型文本
			getTypeText(type) {
				if (typeof type === 'number') {
					// 如果是数字类型，根据需要转换为文本
					switch (type) {
						case 1:
							return '系统消息';
						case 2:
							return '活动通知';
						case 3:
							return '订单消息';
						default:
							return '其他消息';
					}
				}
				return type || '系统消息';
			}
		}
	};
</script>

<style lang="scss">
	.notice-detail-container {
		min-height: 100vh;
		background-color: #f5f5f5;

		.notice-content {
			background-color: #fff;
			padding: 30rpx;

			.notice-header {
				margin-bottom: 30rpx;
				border-bottom: 1rpx solid #eee;
				padding-bottom: 20rpx;

				.notice-title {
					display: block;
					font-size: 36rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 15rpx;
					line-height: 1.4;
				}

				.notice-time {
					display: block;
					font-size: 24rpx;
					color: #999;
					margin-bottom: 10rpx;
				}

				.notice-type {
					display: inline-block;
					font-size: 24rpx;
					color: #ff6b00;
					background-color: #fff5ee;
					padding: 4rpx 16rpx;
					border-radius: 16rpx;
				}
			}

			.notice-body {
				padding: 20rpx 0;

				rich-text {
					font-size: 28rpx;
					color: #666;
					line-height: 1.8;

					p {
						margin-bottom: 20rpx;
					}
				}
			}
		}

		.loading-state,
		.error-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding-top: 300rpx;
			color: #999;
			font-size: 28rpx;
		}

		.retry-btn {
			margin-top: 30rpx;
			background-color: #ff6b00;
			color: #fff;
			font-size: 28rpx;
			padding: 0 40rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
		}
	}
</style>